<!DOCTYPE html>
<html lang="zh-cn">
<head>
  
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>
Colorit——一个给终端输出上色的工具 |
穷折腾</title>
  <link rel="stylesheet" href="/static/css/style.css" />
  <link rel="stylesheet" href="/static/css/pygments.css" />
  <link rel="alternate" type="application/rss+xml" title="RSS" href="http://blog.zorro.im/rss.xml" />
  <link rel="icon" type="image/png" href="/static/img/favicon.png">
</head>
<body>
    <div class="page-wrap">
        <section class="main-header regular">
            <div class="vertical-container">
                <div class="wrapper clearfix">
                    <header>
                        <a href="/">
                            <h1 id="main-title">穷折腾</h1>
                            <h2 id="main-subtitle">zqqf16 的个人博客</h2>
                        </a>
                    </header>
                    <nav id="main-nav" role="navigation">
                        <a href="/" class="selected">主页</a>
                        <a href="/posts/about.html" class="">关于</a>
                        <a href="https://github.com/zqqf16">Github</a>
                    </nav>
                </div>
            </div>
        </section>
        <section class="main-content">
            
<article class="main-content">
    <div class="wrapper">
        <header class="section-header">
            <h1 class="section-title">Colorit——一个给终端输出上色的工具</h1>
            <span class="section-subtitle"><time datetime="2013-11-12" pubdate="">2013-11-12</time></span>
        </header>
        <section class="post-content">
        	<p>平时自己写脚本的时候总喜欢给输出信息加点颜色，比如之前写的查找CVS diff中改动文件的<a href="https://gist.github.com/zqqf16/7094628">脚本</a>。方法很简单，就是输出一些ASCII控制码，比如<code>\033[;31m</code>代表红色之类的。</p>
<p>用的多了，感觉老是拼字符串也不是个办法，索性规整了一下，写了人生中第一个正经的python模块。。。</p>
<div class="codehilite"><pre><span class="c">#!/usr/bin/env python</span>
<span class="c">#-*- coding: utf-8 -*-</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;paint&#39;</span><span class="p">,</span> <span class="s">&#39;colors&#39;</span><span class="p">,</span> <span class="s">&#39;attributes&#39;</span><span class="p">]</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s">&#39;1.0&#39;</span>

<span class="n">_FORMAT</span> <span class="o">=</span> <span class="s">&#39;</span><span class="se">\033</span><span class="s">[{}m</span><span class="se">\033</span><span class="s">[{};{}m{}</span><span class="se">\033</span><span class="s">[0m&#39;</span>

<span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;black&#39;</span><span class="p">,</span> <span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="s">&#39;green&#39;</span><span class="p">,</span> <span class="s">&#39;yellow&#39;</span><span class="p">,</span> <span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="s">&#39;magenta&#39;</span><span class="p">,</span> <span class="s">&#39;cyan&#39;</span><span class="p">,</span> <span class="s">&#39;white&#39;</span><span class="p">]</span>
<span class="n">attributes</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;blod&#39;</span><span class="p">,</span> <span class="s">&#39;underscore&#39;</span><span class="p">,</span> <span class="s">&#39;blink&#39;</span><span class="p">,</span> <span class="s">&#39;reverse&#39;</span><span class="p">,</span> <span class="s">&#39;concealed&#39;</span><span class="p">]</span>

<span class="n">_FOREGROUND</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">colors</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">,</span> <span class="mi">38</span><span class="p">))))</span>
<span class="n">_BACKGROUND</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">colors</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">40</span><span class="p">,</span> <span class="mi">48</span><span class="p">))))</span>
<span class="n">_attributes</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">attributes</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">]))</span>

<span class="k">def</span> <span class="nf">paint</span><span class="p">(</span><span class="n">foreground</span><span class="p">,</span> <span class="n">background</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">attribute</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="n">fg</span> <span class="o">=</span> <span class="n">_FOREGROUND</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">foreground</span><span class="p">,</span> <span class="mi">39</span><span class="p">)</span>
    <span class="n">bg</span> <span class="o">=</span> <span class="n">_BACKGROUND</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">background</span><span class="p">,</span> <span class="mi">49</span><span class="p">)</span>
    <span class="n">att</span> <span class="o">=</span> <span class="n">_attributes</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">attribute</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

    <span class="k">return</span> <span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">_FORMAT</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">att</span><span class="p">,</span> <span class="n">bg</span><span class="p">,</span> <span class="n">fg</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">print_row</span><span class="p">(</span><span class="n">b</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
            <span class="n">p</span> <span class="o">=</span> <span class="n">paint</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
            <span class="k">print</span><span class="p">(</span><span class="n">p</span><span class="p">(</span><span class="s">&#39;{:^8}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">f</span><span class="p">)),</span> <span class="n">end</span><span class="o">=</span><span class="s">&#39; &#39;</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">)</span>

    <span class="n">print_row</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">colors</span><span class="p">:</span>
        <span class="n">print_row</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
</pre></div>


<p>用的时候也很方便：</p>
<div class="codehilite"><pre><span class="n">p</span> <span class="o">=</span> <span class="n">paint</span><span class="p">(</span><span class="n">foreground</span><span class="p">,</span> <span class="n">background</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">attribute</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
<span class="n">p</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
</pre></div>


<p>比如输出字体为绿色，背景为黄色的文字：</p>
<div class="codehilite"><pre><span class="n">p</span> <span class="o">=</span> <span class="n">paint</span><span class="p">(</span><span class="s">&#39;green&#39;</span><span class="p">,</span> <span class="s">&#39;yellow&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">p</span><span class="p">(</span><span class="s">&#39;Hello World&#39;</span><span class="p">))</span>
</pre></div>


<p>可以访问我的<a href="https://github.com/zqqf16/colorit">Github</a>查看完整代码。</p>
<p>最后，附一张完整的颜色图（<code>python colorit.py</code>）：</p>
<p><img alt="colors" src="https://raw.github.com/zqqf16/colorit/master/examples/all.png" /></p>
        </section>
        <div class="post-nav">
    		<!-- Duoshuo Comment BEGIN -->
<div class="ds-thread"></div>
<script type="text/javascript">
	var duoshuoQuery = {short_name:"zqqf16"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = 'http://static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0]
		|| document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
</script>
<!-- Duoshuo Comment END -->

    	</div>
    </div>

</article>

        </section>
    </div>
    <footer class="main-footer">
        <div class="vertical-container">
            <div class="wrapper">
                <p class="copy">&copy; zqqf16</p>
            </div>
        </div>
    </footer>
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-41282906-2', 'auto');
      ga('require', 'displayfeatures');
      ga('send', 'pageview');
    </script>
</body>
</html>
